package logic

import (
	"context"
	"order_srv/cache"
	"order_srv/models"

	"order_srv/internal/svc"
	"order_srv/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *order.GoodsAddRequest) (*order.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var g models.Goods
	g.GoodsTitle = in.GoodsTitle
	g.GoodsPrice = float64(in.GoodsPrice)
	g.GoodsStock = int(in.GoodsStock)
	g.GoodsDesc = in.GoodsDesc
	g.GoodsType = in.GoodsType
	g.Status = int(in.Status)
	if err = g.Create(); err != nil {
		logx.Error("商品创建失败")
		return nil, err
	}
	cache.SyncGoodsStock(int(g.ID), int(in.GoodsStock))
	return &order.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
